//
//  FKOrderListFooter.m
//  WanTao
//
//  Created by Macbook Pro on 2019/3/22.
//  Copyright © 2019 Macbook Pro. All rights reserved.
//

#import "FKOrderListFooter.h"
#import "FKOrder.h"

@interface FKOrderListFooter ()

@property (nonatomic, weak) UILabel *countLabel;
@property (nonatomic, weak) UILabel *priceLabel;
@property (nonatomic, strong) NSArray *itemBtns;

@end

@implementation FKOrderListFooter

- (void)setupSubviews
{
    [super setupSubviews];
    self.contentView.backgroundColor = [UIColor whiteColor];
    
    UILabel *countLabel = [UILabel fk_labelWithFont:fk_adjustFont(12) textColor:fkColor666666];
    [self.contentView addSubview:countLabel];
    self.countLabel = countLabel;
    
    UILabel *priceLabel = [UILabel fk_labelWithFont:fk_adjustBoldFont(15) textColor:fkColor333333];
    [self.contentView addSubview:priceLabel];
    self.priceLabel = priceLabel;
    
    CGFloat margin = fk_adjustW(10);
    CGFloat itemW = fk_adjustW(78);
    CGFloat itemH = fk_adjustW(28);
    CGFloat itemY = fk_adjustW(30);
    CGFloat itemX = fkScreenW - 12 - itemW;
    NSMutableArray *itemBtns = [NSMutableArray array];
    for (NSInteger index = 0; index < 3; index++) {
        UIButton *itemBtn = [UIButton fk_btnWithTarget:self action:@selector(itemBtnClicked:) font:fk_adjustFont(13) normalTitle:nil normalTitleColor:nil selectedTitle:nil selectedTitleColor:nil normalImg:nil selectedImg:nil bgImg:nil selectedBgImg:nil];
        [self.contentView addSubview:itemBtn];
        [itemBtns addObject:itemBtn];
        [itemBtn fk_viewCornerRadius:fk_adjustW(14) borderWidth:0.8 borderColor:nil];
        
        if (index > 0) itemX -= (itemW + margin);
        itemBtn.frame = CGRectMake(itemX, itemY, itemW, itemH);
    }
    self.itemBtns = itemBtns;
    
    [priceLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-12);
        make.top.offset(fk_adjustW(5));
    }];
    [countLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(priceLabel);
        make.right.equalTo(priceLabel.mas_left);
    }];
}

- (void)itemBtnClicked:(UIButton *)btn
{
    if (self.operationBlock) {
        FKOrder *order = (FKOrder *)self.data;
        self.operationBlock(btn.titleLabel.text, order);
    }
}

- (void)setData:(id)data
{
    [super setData:data];
    
    FKOrder *order = (FKOrder *)data;
    self.countLabel.text = order.countText;
    self.priceLabel.text = order.priceText;
    
    for (NSInteger index = 0; index < self.itemBtns.count; index++) {
        UIButton *itemBtn = self.itemBtns[index];
        if (index < order.operationBtnStatuses.count) {
            FKOperationBtnStatus *status = order.operationBtnStatuses[index];
            itemBtn.hidden = false;
            [itemBtn setTitle:status.title forState:UIControlStateNormal];
            [itemBtn setTitleColor:status.themeColor forState:UIControlStateNormal];
            itemBtn.layer.borderColor = status.themeColor.CGColor;
        } else {
            itemBtn.hidden = true;
        }
    }
}

@end
